from research.calcor.pos.move_stop_close import move_stop_close
"""as 
买入5000
止损4900
目标1：5100，移动止损 0.01
目标2：5200，移动止损 0.005
目标3：5300，移动止损 0.025
目标4：5400，移动止损 0.001
"""
class target_stop_close(move_stop_close):
    def __init__(self, argtype="percent", name="", istick=True, stop_loss=None,target_gain=[],atrkey=""):
        super().__init__(argtype, name, istick, stop_loss=stop_loss,atrkey=atrkey)
        self.target_gain=target_gain
        self.step=0
        self.maxsize=len(self.target_gain)
        self.stop_loss0=stop_loss
    def reset(self):
        self.target_price = None
        self.step=0
        self.open_price = None
        self.stop_loss=self.stop_loss0
    def onbar_ss(self,ss,b):
        pass
    def onbar_bar(self,ss,b):
        pass
    def onbar_percent(self,ss,b):
        if self.open_price is None:
            self.open_price=ss.signals_data[-1]["price"]
            self.target_price=self.open_price
        isup = self.update_target_price(ss, b)
        isrun=True
        if isup:#判断目标是否升级
            if self.step<self.maxsize:#是否到最后一个目标
                p0 = self.target_gain[self.step]
                if ss.pos=="buy":
                    p1 = self.open_price * (1 + p0[0])
                    if self.target_price>=p1:
                        self.step=self.step+1
                        self.stop_loss=p0[1]
                        if self.stop_loss==0:
                            isrun=True
                        # print("sys","目标升级buy",self.open_price,self.target_price,p1,self.step,self.stop_loss)
                elif ss.pos=="sell":
                    p1 = self.open_price * (1 - p0[0])
                    if self.target_price<=p1:
                        self.step=self.step+1
                        self.stop_loss=p0[1]
                        if self.stop_loss==0:
                            isrun=True
                        # print("sys", "目标升级sell",self.open_price, self.target_price, p1, self.step, self.stop_loss)

        if self.istick:#判断止损
            isrun=True
        if isrun:
            if ss.pos == "buy":
                stop_loss = self.target_price * (1 - self.stop_loss)
                return self.posbuy_stop_loss(ss, b, stop_loss)
            elif ss.pos == "sell":
                stop_loss = self.target_price * (1 + self.stop_loss)
                return self.possell_stop_loss(ss, b, stop_loss)
        return False,"","",None,None
    def onbar_tick(self,ss,b):
        if self.open_price is None:
            self.open_price=ss.signals_data[-1]["price"]
            self.target_price = self.open_price
        isup = self.update_target_price(ss, b)
        isrun = True
        if isup:  # 判断目标是否升级
            if self.step < self.maxsize:  # 是否到最后一个目标
                p0 = self.target_gain[self.step]
                if ss.pos == "buy":
                    p1 = self.open_price  + p0[0]
                    if self.target_price >= p1:
                        self.step = self.step + 1
                        self.stop_loss = p0[1]
                        if self.stop_loss==0:
                            isrun=True
                elif ss.pos == "sell":
                    p1 = self.open_price  - p0[0]
                    if self.target_price <= p1:
                        self.step = self.step + 1
                        self.stop_loss = p0[1]
                        if self.stop_loss==0:
                            isrun=True
        if self.istick:  # 判断止损
            isrun = True
        if isrun:  # 判断止损
            if ss.pos == "buy":
                stop_loss = self.target_price  - self.stop_loss
                return self.posbuy_stop_loss(ss, b, stop_loss)
            elif ss.pos == "sell":
                stop_loss = self.target_price  + self.stop_loss
                return self.possell_stop_loss(ss, b, stop_loss)
        return False, "", "",None,None
    def onbar_atr(self,ss,b):
        if self.open_price is None:
            self.open_price=ss.signals_data[-1]["price"]
            self.target_price = self.open_price
        atr = b[self.atrkey]
        if atr:
            isup = self.update_target_price(ss, b)
            isrun = True
            if isup:  # 判断目标是否升级
                if self.step < self.maxsize:  # 是否到最后一个目标
                    p0 = self.target_gain[self.step]
                    if ss.pos == "buy":
                        p1 = self.open_price  + p0[0]*atr
                        if self.target_price >= p1:
                            self.step = self.step + 1
                            self.stop_loss = p0[1]
                            if self.stop_loss==0:
                                isrun=True
                            # print("sys", "目标升级buy", self.open_price, self.target_price, p1, self.step, self.stop_loss)
                    elif ss.pos == "sell":
                        p1 = self.open_price  - p0[0]*atr
                        if self.target_price <= p1:
                            self.step = self.step + 1
                            self.stop_loss = p0[1]
                            if self.stop_loss==0:
                                isrun=True
                            # print("sys", "目标升级sell", self.open_price, self.target_price, p1, self.step, self.stop_loss)
            if self.istick:  # 判断止损
                isrun = True
            if isrun:  # 判断止损

                if ss.pos == "buy":
                    stop_loss = self.target_price - self.stop_loss * atr
                    return self.posbuy_stop_loss(ss, b, stop_loss)
                elif ss.pos == "sell":
                    stop_loss = self.target_price + self.stop_loss * atr
                    return self.possell_stop_loss(ss, b, stop_loss)
        return False, "", "",None,None